﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using Core.DataAccess;
using Core.DataAccess.IRepositories;
using Core.DataAccess.Repositories;
using Core.Common;
using Core.Interfaces;
using Core.Interfaces.IService;

namespace Core.Components
{
    [Export(typeof(IAccountService))]
    public class AccountService : IAccountService
    {
        [Import]
        private IAccountRepository _accountRepository;
        [Import]
        private IPermissionRepository _permissionRepository;
        [Import]
        private IUserSession _userSession;
        //[Import]
        //private IEmail _email;

        public AccountService()
        {
            MEFManager.Compose(this);
        }

        public bool UsernameInUse(string Username)
        {
            Account account = _accountRepository.GetAccountByUsername(Username);
            if (account != null)
                return true;

            return false;
        }

        public bool EmailInUse(string Email)
        {
            Account account = _accountRepository.GetAccountByEmail(Email);
            if (account != null)
                return true;

            return false;
        }

        public void Logout()
        {
            _userSession.IsLogged = false;
            _userSession.CurrentUser = null;
            _userSession.UserName = "";
            //_redirector.GoToAccountLoginPage();
        }

        public string Login(string Username, string Password)
        {
            Password = Password.Encrypt(Password);
            Account account = _accountRepository.GetAccountByUsername(Username);

            //if there is only one account returned - good
            if (account != null)
            {
                //password matches
                if (account.Password == Password)
                {
                    //TODO: for development disabling email validation feature
                    if (account.EmailVerified)
                    {
                        _userSession.IsLogged = true;
                        _userSession.UserName = Username;
                        _userSession.CurrentUser = GetAccountByID(account.AccountID);
                        //_redirector.GoToHomePage();
                    }
                    else
                    {
                        //_email.SendEmailAddressVerificationEmail(account.Username, account.Email);
                        return @"The login information you provided was correct 
                                                    but your email address has not yet been verified.  
                                                    We just sent another email verification email to you.  
                                                    Please follow the instructions in that email.";
                    }
                }
                else
                {
                    return "Your Password seems to be incorrect. Try again!";
                }
            }

            return "Check your Username and try again!";
        }


        public Account GetAccountByID(Int32 AccountID)
        {
            Account account = _accountRepository.GetAccountByID(AccountID);
            List<Permission> permissions = _permissionRepository.GetPermissionsByAccountID(AccountID);
            foreach (Permission permission in permissions)
            {
                account.AddPermission(permission);
            }

            return account;
        }
    }
}
